package io.zcoder.innoway.adapters;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

import java.util.HashMap;
import java.util.Map;

import io.zcoder.innoway.activities.EntryDetailActivity;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.fragments.EntryBMIFragment;
import io.zcoder.innoway.fragments.EntryOtherFragment;
import io.zcoder.innoway.fragments.TrendDetailFragment;
import io.zcoder.innoway.models.Dashboard;
import io.zcoder.innoway.models.Profile;

public class ViewPagerTrendAdapter extends FragmentPagerAdapter {

    private Profile mProfile;
    private Map<Integer, TrendDetailFragment> mFragmentMap = new HashMap<>();

    public ViewPagerTrendAdapter(Dashboard dashboard, FragmentManager fm, Profile profile) {
        super(fm);
        mProfile = profile;
    }

    @Override
    public Fragment getItem(int position) {
        if (!mFragmentMap.containsKey(position)) {
            Bundle bundle = new Bundle();
            if (mProfile != null) {
                bundle.putParcelable(Constants.PROFILE_EXTRAS, mProfile);
            }
            bundle.putInt(Constants.INDEX_EXTRAS, position);
            TrendDetailFragment fragment = new TrendDetailFragment();
            fragment.setArguments(bundle);
            mFragmentMap.put(position, fragment);
        }
        return mFragmentMap.get(position);
    }

    @Override
    public int getCount() {
        return 8;
    }

    public void updateProfile(Profile profile) {
        mProfile = profile;
        notifyDataSetChanged();
    }

    public void updateFragments() {
        for (int i = 0; i < getCount(); i++) {
            mFragmentMap.get(i).getData();
        }
    }
}
